package com.ljx.stream.stream01;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.*;
import java.util.stream.Stream;

/**
 * @author LJX
 * @Description: T获取stream流方式
 * @Date: 2022/11/4 16:36
 * @Version 1.0
 */
public class Stream03 {
	public static void main(String[] args) {
		// 1通过Collection集合对象中的stream方法获取，或者parallelStream
		/*	List<String> list = new ArrayList<>();
		Collections.addAll(list,"ljx","ljl","hjy","lhf");
		Stream<String> stream = list.stream();
		// 通过lambda遍历集合
		stream.forEach(e-> System.out.println(e));
		// 通过方法的引用遍历集合(因为上面已经将stream流foreach[终止操作了]所以下面再使用stream流会出异常)
		stream.forEach(System.out::println);
		// 并行流
		Stream<String> stringStream = list.parallelStream();
		stringStream.forEach(System.out::println);*/

		// 2,通过Arrays转化流
		/*int[] arr={123,231,23,43};
		IntStream stream = Arrays.stream(arr);
		stream.forEach(System.out::println);*/

		// 3通过Stream中of iterator,generator();
		/*Stream<String> list = Stream.of("java01", "java02", "java03");
		long count = list.count();
		System.out.println(count);*/

		// 中间操作和终止操作
		ArrayList<Emp> List = new ArrayList<>();
		List<Emp> list=new ArrayList<>();
		list.add(new Emp("小红",18000.0));
		list.add(new Emp("小刚",28000.0));
		list.add(new Emp("小绿",8000.0));
		list.add(new Emp("小白",12000.0));
		list.add(new Emp("小微",10000.0));
		list.add(new Emp("小超",100000.0));
		list.add(new Emp("小明",1000.0));
		list.add(new Emp("小明",1000.0));

		Stream<Emp> stream = list.stream();

		// filter过滤 月薪不足10000
		/*
		long count = stream.filter(t -> t.getSalary() < 10000).count();
		System.out.println(count);*/
		// 限制limit
		/*stream.limit(3).forEach(System.out::println);
*/
		// 跳过开头两个
		/*stream.skip(2).forEach(System.out::println);
		* */
		// 去除重复
		/*stream.distinct().forEach(System.out::println);
*/
		//sorted方法中要跟比较规则（Stream<T> sorted(Comparator<? super T> comparator)）
		/*stream.sorted((o1,o2)-> (int) (o1.getSalary()-o2.getSalary())).forEach(System.out::println);
*/
		//map:映射  R apply(T t);--->结构发生改变
		/*stream.map(Emp::getName).forEach(System.out::println);
		*/

		//串行和并行的流比较
		/*	List<String> list1=new ArrayList<>();
		for (int i = 0; i <5000000 ; i++) {
			list1.add(UUID.randomUUID().toString());
		}
		long l1 = System.currentTimeMillis();
		list.stream().sorted().count();
//        list.parallelStream().sorted().count();
		long l2 = System.currentTimeMillis();
		System.out.println("耗时:"+(l2-l1));*/

		// 终止操作 foreach,min,max ,count
		/*Optional<Emp> min = stream.min((o1, o2) -> (int) (o1.getSalary() - o2.getSalary()));
		Optional<Emp> max = stream.max((o1, o2) -> (int) (o1.getSalary() - o2.getSalary()));
		Emp emp = min.get();
		Emp emp1 = max.get();
		System.out.println(emp1);
*/
	}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class Emp{
	private String name;
	private double salary;
}